home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / efs / dired-vir.el.z / dired-vir.el
Encoding:
Text File  |  1998-05-21  |  4.9 KB  |  139 lines

  1. ;;  -*-Emacs-Lisp-*-
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;;
  4. ;; File:          dired-vir.el
  5. ;; Dired Version: #Revision: 7.9 $
  6. ;; RCS:
  7. ;; Description:   Virtual dired mode for browsing ls -lR listings.
  8. ;; Author:        Sebastian Kremer <sk@thp.uni-koeln.de>
  9. ;; Created:       7-Mar-1991 16:00
  10. ;;
  11. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  12.  
  13. ;;; Requirements and provisions
  14. (provide 'dired-vir)
  15. (require 'dired)
  16.  
  17. (defun dired-virtual (dirname &optional switches)
  18.   "Put this buffer into Virtual Dired mode.
  19.  
  20. In Virtual Dired mode, all commands that do not actually consult the
  21. filesystem will work.
  22.  
  23. This is useful if you want to peruse and move around in an ls -lR
  24. output file, for example one you got from an ftp server.  With
  25. efs, you can even dired a directory containing an ls-lR file,
  26. visit that file and turn on virtual dired mode.  But don't try to save
  27. this file, as dired-virtual indents the listing and thus changes the
  28. buffer.
  29.  
  30. If you have save a Dired buffer in a file you can use \\[dired-virtual] to
  31. resume it in a later session.
  32.  
  33. Type \\<dired-mode-map>\\[revert-buffer] in the
  34. Virtual Dired buffer and answer `y' to convert the virtual to a real
  35. dired buffer again.  You don't have to do this, though: you can relist
  36. single subdirs using \\[dired-do-redisplay].
  37. "
  38.  
  39.   ;; DIRNAME is the top level directory of the buffer.  It will become
  40.   ;; its `default-directory'.  If nil, the old value of
  41.   ;; default-directory is used.
  42.  
  43.   ;; Optional SWITCHES are the ls switches to use.
  44.  
  45.   ;; Shell wildcards will be used if there already is a `wildcard'
  46.   ;; line in the buffer (thus it is a saved Dired buffer), but there
  47.   ;; is no other way to get wildcards.  Insert a `wildcard' line by
  48.   ;; hand if you want them.
  49.  
  50.   (interactive
  51.    (list (read-string "Virtual Dired directory: " (dired-virtual-guess-dir))))
  52.   (goto-char (point-min))
  53.   (or (looking-at "  ")
  54.       ;; if not already indented, do it now:
  55.       (indent-region (point-min) (point-max) 2))
  56.   (or dirname (setq dirname default-directory))
  57.   (setq dirname (expand-file-name (file-name-as-directory dirname)))
  58.   (setq default-directory dirname)    ; contains no wildcards
  59.   (let ((wildcard (save-excursion
  60.             (goto-char (point-min))
  61.             (forward-line 1)
  62.             (and (looking-at "^  wildcard ")
  63.              (buffer-substring (match-end 0)
  64.                        (progn (end-of-line) (point)))))))
  65.   (if wildcard
  66.     (setq dirname (expand-file-name wildcard default-directory))))
  67.   ;; If raw ls listing (not a saved old dired buffer), give it a
  68.   ;; decent subdir headerline:
  69.   (goto-char (point-min))
  70.   (or (looking-at dired-subdir-regexp)
  71.       (dired-insert-headerline default-directory))
  72.   (dired-mode dirname (or switches dired-listing-switches))
  73.   (setq mode-name "Virtual Dired"
  74.     revert-buffer-function 'dired-virtual-revert)
  75.   (set (make-local-variable 'dired-subdir-alist) nil)
  76.   (dired-build-subdir-alist)
  77.   (goto-char (point-min))
  78.   (dired-insert-set-properties (point-min) (point-max))
  79.   (dired-initial-position dirname))
  80.  
  81. (defun dired-virtual-guess-dir ()
  82.  
  83.   ;; Guess and return appropriate working directory of this buffer,
  84.   ;; assumed to be in Dired or ls -lR format.
  85.   ;; The guess is based upon buffer contents.
  86.   ;; If nothing could be guessed, returns nil.
  87.  
  88.   (let ((regexp "^\\(  \\)?\\([^ \n\r]*\\)\\(:\\)[\n\r]")
  89.     (subexpr 2))
  90.     (goto-char (point-min))
  91.     (cond ((looking-at regexp)
  92.        ;; If a saved dired buffer, look to which dir and
  93.        ;; perhaps wildcard it belongs:
  94.        (let ((dir (buffer-substring (match-beginning subexpr)
  95.                     (match-end subexpr))))
  96.          (file-name-as-directory dir)))
  97.       ;; Else no match for headerline found.  It's a raw ls listing.
  98.       ;; In raw ls listings the directory does not have a headerline
  99.       ;; try parent of first subdir, if any
  100.       ((re-search-forward regexp nil t)
  101.        (file-name-directory
  102.         (directory-file-name
  103.          (file-name-as-directory
  104.           (buffer-substring (match-beginning subexpr)
  105.                 (match-end subexpr))))))
  106.       (t                ; if all else fails
  107.        nil))))
  108.  
  109.  
  110. (defun dired-virtual-revert (&optional arg noconfirm)
  111.   (if (not
  112.        (y-or-n-p "Cannot revert a Virtual Dired buffer - switch to Real Dired mode? "))
  113.       (error "Cannot revert a Virtual Dired buffer.")
  114.     (setq mode-name "Dired"
  115.       revert-buffer-function 'dired-revert)
  116.     (revert-buffer)))
  117.  
  118. ;; A zero-arg version of dired-virtual.
  119. ;; You need my modified version of set-auto-mode for the
  120. ;; `buffer-contents-mode-alist'.
  121. ;; Or you use infer-mode.el and infer-mode-alist, same syntax.
  122. (defun dired-virtual-mode ()
  123.   "Put current buffer into virtual dired mode (see `dired-virtual').
  124. Useful on `buffer-contents-mode-alist' (which see) with the regexp
  125.  
  126.     \"^  \\(/[^ /]+\\)/?+:$\"
  127.  
  128. to put saved dired buffers automatically into virtual dired mode.
  129.  
  130. Also useful for `auto-mode-alist' (which see) like this:
  131.  
  132.   \(setq auto-mode-alist (cons '(\"[^/]\\.dired$\" . dired-virtual-mode)
  133.                   auto-mode-alist)\)
  134. "
  135.   (interactive)
  136.   (dired-virtual (dired-virtual-guess-dir)))
  137.  
  138. ;;; end of dired-vir.el
  139.